<?php
/**
 * @date 2012-4-30
 * @author GhostWei
 */
class controller_admin_catalog_manufacturer extends system_controller_action {
	
	private $user = null;
	private $referer = null;
	private $language = null;
	public function init() {
		if($this->adminLogin()) {
			return;
		}
		$this->view->language = $this->language = $this->view->language();
		$this->view->user = $this->user = system_user::getInstance()->getRow();
		$this->referer = $this->getRequest()->getHeader('referer');
		
		$this->crumbs()
			->addPage(system_nav_page::create($this->language->load('Catalog'))->setRoute('catalog/index'))
			->addPage(system_nav_page::create($this->language->load('Manufacturer'))->setRoute('catalog/manufacturer/index'));
	}
	
	public function action_index() {
		$this->redirector()->gotoRouteAndExit('catalog/manufacturer/list');
		$this->render();
	}
	
	public function action_addnew() {
		$concept = new catalog_manufacturer_concept();
		$row = $concept->create();
		$row->setDisplayOrder(0);
		$row->setIsActive(1);		
		$row = $this->_saveRow($row);		
		$this->view->row = $row;
		$this->view->referer = $this->referer;
		$this->crumbs()->addPage(system_nav_page::create($this->language->load('New Manufacturer'))->setRoute('catalog/manufacturer/addnew'));
		$this->render('catalog/manufacturer/row.phtml');
	}

	public function action_edit() {
		$id = intval($this->getRequest()->getParam('id'));
		$concept = new catalog_manufacturer_concept();
		if(!($row = $concept->fetch(array('manufacturer.id = ?' => $id)))) {
			throw new catalog_exception("This Manufacturer is not existing.", 404);
		}
		$row = $this->_saveRow($row);
		$this->view->row = $row;
		$this->view->referer = $this->referer;
		$this->crumbs()->addPage(system_nav_page::create($this->language->load('Edit'))->setRoute('catalog/manufacturer/edit')->setParams(array('id' => $id)));
		$this->render('catalog/manufacturer/row.phtml');
	}
	
	private function _saveRow($row) {
		$this->crumbs()->addPage(system_nav_page::create($this->language->load('List'))->setRoute('catalog/manufacturer/list'));
		if(!$this->getRequest()->isPost()) {
			return $row;
		}
		$this->referer = $this->getRequest()->getPost('referer');
		$row->setUrl(trim($this->getRequest()->getPost('url')));
		$row->setName(trim($this->getRequest()->getPost('name')));
		$row->setEname(trim($this->getRequest()->getPost('ename')));
		$row->setDisplayOrder(intval($this->getRequest()->getPost('display_order')));
		$row->setDescription($this->getRequest()->getPost('description'));
		$row->setIsActive(intval($this->getRequest()->getPost('is_active')) ? 1 : 0);
		try {
			if($this->getRequest()->getPost('delete')) {
				$row->delete();
			} else {
				if(!empty($_FILES["thumb"]['tmp_name'])) {
					common_media_config::$imageFilter = array('jpg', 'png', 'gif');
					$thumb = common_media_image::create($_FILES["thumb"]);
					if($thumb) {
						$row->setThumb($thumb->getMd5());
					}
					$thumb->save();
				}
				if($this->getRequest()->getPost('deleteImage')) {
					$row->setThumb('');
				}
				$row = $row->save();
			}
			$this->view->throwMessage = 'Complete';
		} catch (Exception $e) {
			$this->view->throwException = $e;
		}
		return $row;
	}

	public function action_list() {
		$adapter = new system_paginator_adapter_concept(new catalog_manufacturer_concept());
		$adapter->setCurrentPageNumber(intval($this->getRequest()->getParam('page', 1)));
		//$adapter->where($where);
		$adapter->order("manufacturer.lastmodified DESC");
		$adapter->setLimit(30);		
		$this->view->list = new system_paginator($adapter);
		
		$this->crumbs()->addPage(system_nav_page::create($this->language->load('List'))->setRoute('catalog/manufacturer/list'));
		$this->render();
	}
	
}
